package com.fr.web.core;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.ConfigManager;
import com.fr.util.Consts;
import com.fr.web.ErrorHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DefaultErrorHandler
  implements ErrorHandler
{
  public void error(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, Throwable paramThrowable)
  {
    error(paramHttpServletRequest, paramHttpServletResponse, paramThrowable.getMessage(), paramThrowable);
  }

  public void error(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
  {
    error(paramHttpServletRequest, paramHttpServletResponse, paramString, null);
  }

  public void error(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString, Throwable paramThrowable)
  {
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    if (paramString == null)
      paramString = "";
    localMap.put("message", CodeUtils.htmlEncode(paramString));
    localMap.put("duration", Consts.HISTORY);
    localMap.put("companyname", Consts.COMPANY_NAME);
    try
    {
      Object localObject;
      if (paramHttpServletRequest.getParameter("frload") != null)
      {
        localObject = WebUtils.createPrintWriter(paramHttpServletResponse);
        ((PrintWriter)localObject).println(new StringBuffer().append("alert(\"").append(paramString.replaceAll("\"", "\\\\\"")).append("\")"));
        ((PrintWriter)localObject).flush();
        ((PrintWriter)localObject).close();
        return;
      }
      if (paramThrowable != null)
      {
        localObject = new StringWriter();
        PrintWriter localPrintWriter = new PrintWriter((Writer)localObject);
        paramThrowable.printStackTrace(localPrintWriter);
        localMap.put("exception", CodeUtils.htmlEncode(((StringWriter)localObject).toString()));
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      localMap.put("charset", CodeUtils.htmlEncode(FRContext.getConfigManager().getServerCharset()));
      TemplateUtils.dealWithTemplate("/com/fr/web/core/error.html", paramHttpServletResponse, localMap);
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.SEVERE, localIOException.getMessage(), localIOException);
    }
  }
}